package com.flexdms.i18n;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;



public class TextRenderer extends BasicRenderer
{
	
	@Override
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException
	{

		if (!component.isRendered())
		{
			return;
		}

		ResponseWriter writer = context.getResponseWriter();
		writer.startElement("span", component);
		writer.writeAttribute("id",  component.getClientId(context),
                "id");
		writeGenericAttributes(component, writer);
		String styleClass = (String) component.getAttributes()
				.get("styleClass");
		if (null != styleClass)
		{
			writer.writeAttribute("class", styleClass, "styleClass");
		}

		// render the curentValue as label text if specified.
		String value = getLocalizedText(component);
		if (value != null && value.length() != 0)
		{
			Object val = component.getAttributes().get("escape");
			boolean escape = (val != null) && Boolean.valueOf(val.toString());

			if (escape)
			{
				writer.writeText(value, component, "value");
			} else
			{
				writer.write(value);
			}
		}
		writer.flush();

	}

	@Override
	public void encodeEnd(FacesContext context, UIComponent component)
			throws IOException
	{

		if (!component.isRendered())
		{
			return;
		}
		ResponseWriter writer = context.getResponseWriter();
		writer.endElement("span");

	}

}
